package com.trade.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.trade.form.ProcurmentContractForm;
import com.trade.model.ProcurmentContract;
import com.trade.service.IProcurmentContractService;

/**
 * @author Lin Yang
 * 
 */
public class ProcurmentContractAction extends DispatchAction {
    private IProcurmentContractService procurmentContractService;

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ProcurmentContractForm procurmentContactForm = (ProcurmentContractForm) form;

        ProcurmentContract procurmentContact = new ProcurmentContract();
        procurmentContact.setName(procurmentContactForm.getName());

        procurmentContact.setGoodsId(procurmentContactForm.getGoodsId());
        procurmentContact.setGoodsName(procurmentContactForm.getGoodsName());

        procurmentContact.setSupplierId(procurmentContactForm.getSupplierId());
        procurmentContact.setSupplierName(procurmentContactForm.getSupplierName());

        procurmentContact.setContent(procurmentContactForm.getContent());
        procurmentContact.setContractTime(System.currentTimeMillis());

        procurmentContact.setGoodsNumber(procurmentContactForm.getGoodsNumber());
        procurmentContact.setGoodsPrice(procurmentContactForm.getGoodsPrice());
        procurmentContact.setTotalCost(procurmentContactForm.getTotalCost());

        procurmentContact.setState(ProcurmentContract.STATE_SUBMIT);

        procurmentContact.setBuyerLoginName((String) request.getSession().getAttribute("userName"));
        procurmentContact.setBuyerName((String) request.getSession().getAttribute("actualName"));

        procurmentContractService.saveProcurmentContract(procurmentContact);

        return mapping.findForward("success");
    }

    public ActionForward approve(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String strId = request.getParameter("id");
        long id = Long.parseLong(strId);
        ProcurmentContract contract = procurmentContractService.getProcurmentContractById(id);
        contract.setApproveTime(System.currentTimeMillis());
        contract.setApproverLoginName((String) request.getSession().getAttribute("userName"));
        contract.setApproverName((String) request.getSession().getAttribute("actualName"));
        procurmentContractService.approveContract(contract);
        return mapping.findForward("success");
    }

    public ActionForward verify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        long id = Long.parseLong(request.getParameter("id"));
        ProcurmentContract contract = procurmentContractService.getProcurmentContractById(id);
        contract.setVerifyTime(System.currentTimeMillis());
        contract.setVerifierLoginName((String) request.getSession().getAttribute("userName"));
        contract.setVerifierName((String) request.getSession().getAttribute("actualName"));
        procurmentContractService.verifyContract(contract);
        return mapping.findForward("success");
    }

    public void setProcurmentContractService(IProcurmentContractService procurmentContractService) {
        this.procurmentContractService = procurmentContractService;
    }

}
